package ext.generic.erp.config;

import java.rmi.RemoteException;
import java.util.Iterator;
import java.util.List;

import ext.com.iba.IBAUtil;
import ext.generic.erp.bean.ERPPartTemplateBean;
import ext.generic.erp.common.CommonPDMUtil;
import ext.generic.erp.util.IntegrationConstant;
import ext.generic.erp.util.PDMIntegrationLogUtil;
import wt.part.WTPart;
import wt.util.WTException;

public class ERPPartTemplateUtil {
	
	private static String clazz = ERPPartTemplateUtil.class.getName() ;
	
	private static boolean verbose = PDMIntegrationLogUtil.verbose ;
	
	private static final String POL_TYPE = "POL" ;
	
	public static String getERPPartCategory( WTPart part ){
		String erpPartCategory = "" ;
		
		ERPPartTemplateBean erpPartTemplateBean = getERPPartTemplateBean( part ) ;
		
		if( erpPartTemplateBean != null ){
			erpPartCategory = erpPartTemplateBean.getErpPartCategory() ;
			
			PDMIntegrationLogUtil.printLog(clazz, "getERPPartCategory( WTPart part ) , erpPartCategory = " + erpPartCategory , IntegrationConstant.LOG_LEVEL_INFO ) ;
			
		} else{
			PDMIntegrationLogUtil.printLog(clazz, "getERPPartCategory( WTPart part ) , erpPartTemplateBean == null ", IntegrationConstant.LOG_LEVEL_ERROR ) ;
		}
		
		return erpPartCategory ;
	}
	
	public static String getERPPartTemplate( WTPart part ){
		String erpPartTemplate = "" ;
		
		ERPPartTemplateBean erpPartTemplateBean = getERPPartTemplateBean( part ) ;
		
		if( erpPartTemplateBean != null ){
			erpPartTemplate = erpPartTemplateBean.getErpPartTemplate() ;
			
			PDMIntegrationLogUtil.printLog(clazz, "getERPPartTemplate( WTPart part ) , erpPartTemplate = " + erpPartTemplate , IntegrationConstant.LOG_LEVEL_INFO ) ;
		} else{
			PDMIntegrationLogUtil.printLog(clazz, "getERPPartTemplate( WTPart part ) , erpPartTemplateBean == null ", IntegrationConstant.LOG_LEVEL_ERROR ) ;
		}
		
		return erpPartTemplate ;
	}
	
	private static ERPPartTemplateBean getERPPartTemplateBean( WTPart part ){
		ERPPartTemplateBean erpPartTemplateBean = null ;
		
		if( part == null ){
			return erpPartTemplateBean ;
		}
		
		List<ERPPartTemplateBean> erpPartTemplateBeanList = null ;
		String pdmPartENameOrSource = "" ;
		String pdmPartCode = "" ;
		
		String softTypeInternalValue = CommonPDMUtil.getSoftTypeInternal(part);
		
		//原材料
		if( softTypeInternalValue.equalsIgnoreCase("RawMaterial") ){
			erpPartTemplateBeanList = ERPPartTemplateConfigExcelUtil.getInstance().getRawMaterialList() ;
			
			try {
				pdmPartCode = part.getNumber() ;
				
				//如果编码长度不小于3，则截取编码前3位
				if(pdmPartCode != null){
					if( pdmPartCode.contains( POL_TYPE ) ){
						pdmPartCode = POL_TYPE ;
					}else{
						if( ! ( pdmPartCode.length() < 3 ) ){
							pdmPartCode = pdmPartCode.substring(0, 3) ;
						}
					}
				}
				
				//获取英文名称
				pdmPartENameOrSource = (String) IBAUtil.getIBAValue( part , "partEName" );
			} catch (RemoteException e) {
				e.printStackTrace();
			} catch (WTException e) {
				e.printStackTrace();
			}
		}
		//半成品
		else if( softTypeInternalValue.equalsIgnoreCase("SemiFinishedGood") ){
			erpPartTemplateBeanList = ERPPartTemplateConfigExcelUtil.getInstance().getSemiFinishedGoodList() ;
			
			try {
				//获取半成品类型
				pdmPartCode = (String) IBAUtil.getIBAValue(part, "SemiFinishedGoodType") ;
				
				//获取半成品源
				pdmPartENameOrSource = CommonPDMUtil.getSourceCN(part) ;
			} catch (RemoteException e) {
				e.printStackTrace();
			} catch (WTException e) {
				e.printStackTrace();
			}
		}
		//成品
		else if(softTypeInternalValue.equals("FinishedGood")){
			erpPartTemplateBeanList = ERPPartTemplateConfigExcelUtil.getInstance().getFinishedGoodList() ;
			
			try {
				//获取成品类型
				pdmPartCode = (String) IBAUtil.getIBAValue(part, "FinishedGoodType") ;
				
				//获取成品源
				pdmPartENameOrSource = CommonPDMUtil.getSourceCN(part) ;
			} catch (RemoteException e) {
				e.printStackTrace();
			} catch (WTException e) {
				e.printStackTrace();
			}
			
		} else{
			PDMIntegrationLogUtil.printLog(clazz, "getERPPartTemplateBeanList( WTPart part ) , softTypeInternalValue = " + softTypeInternalValue , IntegrationConstant.LOG_LEVEL_ERROR ) ;
		}
		
		printLogInfo(part, pdmPartCode , pdmPartENameOrSource );
		
		erpPartTemplateBean = getERPPartTemplateBean( erpPartTemplateBeanList, pdmPartCode , pdmPartENameOrSource );
		
		return erpPartTemplateBean ;
	}

	private static ERPPartTemplateBean getERPPartTemplateBean(List<ERPPartTemplateBean> erpPartTemplateBeanList , String pdmPartCode , String pdmPartENameOrSource ) {

		if( erpPartTemplateBeanList != null ){
			Iterator<ERPPartTemplateBean> ite = erpPartTemplateBeanList.iterator() ;
			
			while( ite.hasNext() ){
				ERPPartTemplateBean currentERPPartTemplateBean = ite.next() ;
				
				if( currentERPPartTemplateBean != null ){
					String currentPDMPartCode = currentERPPartTemplateBean.getPdmPartCode() ;
					String currentPDMPartENameOrSource = currentERPPartTemplateBean.getPdmPartENameOrSource() ;
					
					//如果pdmPartCode和pdmPartENameOrSource相同，则返回
					if( currentPDMPartCode.equalsIgnoreCase( pdmPartCode ) && currentPDMPartENameOrSource.equalsIgnoreCase( pdmPartENameOrSource )){
						return currentERPPartTemplateBean;
					}
				}
			}
		}
		
		return null ;
	}

	private static void printLogInfo(WTPart part , String pdmPartCode , String pdmPartENameOrSource ) {
		if( verbose ){
			StringBuffer logInfo = new StringBuffer("") ;
			
			logInfo.append("getERPPartTemplateBeanList( WTPart part ) : \n") ;
			logInfo.append(CommonPDMUtil.getPartInfo(part));
			logInfo.append("pdmPartCode = ") ;
			logInfo.append(pdmPartCode);
			logInfo.append(" , pdmPartENameOrSource = ") ;
			logInfo.append(pdmPartENameOrSource) ;
			
			PDMIntegrationLogUtil.printLog(clazz, logInfo.toString() , IntegrationConstant.LOG_LEVEL_NORMAL ) ;
		}
	}
}
